package com.rio.dandan.action;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rio.dandan.pojo.UserAccount;
import com.rio.dandan.pojo.UserProfile;
import com.rio.dandan.pojo.UserScore;
import com.rio.dandan.pojo.UserVideo;
import com.rio.dandan.pojo.UserVoice;
import com.rio.dandan.service.UserAccountService;
import com.rio.dandan.service.UserPhotoService;
import com.rio.dandan.service.UserProfileService;
import com.rio.dandan.service.UserScoreService;
import com.rio.dandan.service.UserVideoService;
import com.rio.dandan.service.UserVoiceService;
import com.rio.dandan.util.IpUtils;
import com.rio.dandan.util.JsonUtils;
import com.rio.dandan.util.SecurityUtils;
import com.rio.dandan.util.SessionUtils;

/**
 * 
 * UserAccountController.java
 * 
 * @author 宋陆
 * @version 1.0 2014年5月18日
 */
@Controller
public class UserAccountController {
    // private static final Logger log = LoggerFactory.getLogger("SYS");
    @Resource
    UserAccountService userAccountService;
    @Resource
    UserProfileService userProfileService;
    @Resource
    UserScoreService userScoreService;
    @Resource
    UserPhotoService userPhotoService;
    @Resource
    UserVoiceService userVoiceService;
    @Resource
    UserVideoService userVideoService;

    @RequestMapping("/register.htm")
    public String register(Map<String, Object> model) {
        return "user_account/register";
    }

    @RequestMapping("/login.htm")
    public String login(Map<String, Object> model) {
        return "user_account/login";
    }

    @RequestMapping("/register.json")
    public Object registerAction(UserAccount userAccount, UserProfile userProfile) {
        String strIp = SessionUtils.getIpAddr();
        Long longIp = IpUtils.ipToLong(strIp);
        Date now = new Date();
        try {
            userAccount.setRegTime(now);
            userAccount.setRegIpStr(strIp);
            userAccount.setRegIpLong(longIp);
            userAccount.setLastTime(now);
            userAccount.setLastIpStr(strIp);
            userAccount.setLastIpLong(longIp);
            userAccount.setLoginTime(now);
            userAccount.setLoginIpStr(strIp);
            userAccount.setLoginIpLong(longIp);
            Date birthday = userProfile.getBirthday();
            Calendar c = Calendar.getInstance();
            c.setTime(birthday);
            int birthYear = c.get(Calendar.YEAR);
            int birthMonth = c.get(Calendar.MONTH);
            int birthDay = c.get(Calendar.DATE);
            userProfile.setBirthYear(birthYear);
            userProfile.setBirthMonth(birthMonth);
            userProfile.setBirthDay(birthDay);
            userAccountService.addUserAccount(userAccount, userProfile);
            return ActionResult.success(JsonUtils.toJson(userAccount));
        } catch (Exception ex) {
            return ActionResult.failure(ex.getMessage());
        }
    }

    @RequestMapping("/login.json")
    public Object loginAction(String username, String password) {
        try {
            userAccountService.login(username, password);
            return ActionResult.success();
        } catch (Exception ex) {
            return ActionResult.failure(ex.getMessage());
        }
    }

    @RequestMapping("/logout.do")
    public String logout() {
        SecurityUtils.logout();
        return "redirect:login.htm";
    }

    @RequestMapping("/center.htm")
    public String center(Map<String, Object> model) {

        String uid = SecurityUtils.getCurrUId();
        /** 用户账户 */
        UserAccount userAccount = userAccountService.getUserAccount(uid);
        /** 用户资料 */
        UserProfile userProfile = userProfileService.getUserProfile(uid);
        /** 用户积分 */
        UserScore userScore = userScoreService.getUserScore(uid);
        /** 用户相册 */
//        List<UserAlbum> userPhotoList = userPhotoService.findUserPhoto(uid);
        /** 用户音频 */
        List<UserVoice> userVoiceList = userVoiceService.findUserVoice(uid);
        /** 用户视频 */
        List<UserVideo> userVideoList = userVideoService.findUserVideo(uid);
        model.put("userAccount", userAccount);
        model.put("userProfile", userProfile);
        model.put("userScore", userScore);
//        model.put("userPhotoList", userPhotoList);
        model.put("userVoiceList", userVoiceList);
        model.put("userVideoList", userVideoList);
        return "user_account/center";
    }

    @RequestMapping("/home.htm")
    public String home(Map<String, Object> model, String uid) {
        /** 用户账户 */
        UserAccount userAccount = userAccountService.getUserAccount(uid);
        /** 用户资料 */
        UserProfile userProfile = userProfileService.getUserProfile(uid);
        /** 用户相册 */
//        List<UserAlbum> userPhotoList = userPhotoService.findUserPhoto(uid);
        /** 用户音频 */
        List<UserVoice> userVoiceList = userVoiceService.findUserVoice(uid);
        /** 用户视频 */
        List<UserVideo> userVideoList = userVideoService.findUserVideo(uid);
        model.put("userAccount", userAccount);
        model.put("userProfile", userProfile);
//        model.put("userPhotoList", userPhotoList);
        model.put("userVoiceList", userVoiceList);
        model.put("userVideoList", userVideoList);
        return "user_account/home";
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
        binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, false));
        binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, null, true));
        binder.registerCustomEditor(int.class, null, new CustomNumberEditor(Integer.class, null, true));
        binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, null, true));
        binder.registerCustomEditor(long.class, null, new CustomNumberEditor(Long.class, null, true));
        binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, true));
        binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
        binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));
    }

}
